package com.tierstall.client.view.content;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.gwt.user.client.ui.Widget;
import com.tierstall.client.presenter.GroupMGMTPresenter;
import com.tierstall.client.view.content.CV_showUsers.CwConstants;
import com.tierstall.client.view.main.ContentWidget;
import com.tierstall.shared.GruppeDTO;
import com.tierstall.shared.Page;
import com.tierstall.shared.TierDTO;
import com.tierstall.shared.TierGender;
import com.tierstall.shared.UserType;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.SuggestBox;
import com.tierstall.shared.Logger;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

public class CV_addTier extends ContentWidget {
	public interface AddTierPresenter {
		// Presenter Methods here
		void onAddClicked(TierDTO tier);

		void groupSelected(GruppeDTO group);
	}

	private AddTierPresenter presenter;

	public void setPresenter(AddTierPresenter presenter) {
		this.presenter = presenter;
	}

	// standard stuff

	Button btn_add_group;

	ListBox groupCombo;

	DateBox eingangDate;

	DateBox dispatchDate;

	TextBox originTextBox;

	TextBox stammTextBox;

	ListBox genderCombo;

	TextBox typusText;

	DateBox birthdayDate;

	ListBox fatherCombo;

	ListBox motherCombo;

	IntegerBox intNrIntBox;

	TextArea noteTextBox;

	SuggestBox intIdSuggestBox;

	TextBox speciesBox;

	FlexTable layout;
	HorizontalPanel genderPanel;

	// interface CView_addGroupUiBinder extends
	// UiBinder<Widget, CView_addGroup> {
	// }

	public CV_addTier() {
		super("Neues Tier", "Ein neues Tier in die Datenbank aufnehmen", false,
				UserType.EDITOR, Page.ADD_TIER);
		// initWidget(uiBinder.createAndBindUi(this));
	}

	public void initForm() {
		Logger.CLIENT_INFO("Initializing View: AddTier...");
		groupCombo.clear();
		groupCombo.addItem("Lade Daten...");
		Logger.CLIENT_INFO("disabling speciesBox");
		speciesBox.setEnabled(false);
	}

	public void fillGroupCombo(final ArrayList<GruppeDTO> groups) {
		groupCombo.clear();
		groupCombo.addItem("Bitte ausw&auml;hlen...");
		for (int i = 0; i < groups.size(); i++) {
			groupCombo.addItem(groups.get(i).getGenehmigung() + " / PL: "
					+ groups.get(i).getLeiter(), groups.get(i).getUuid());
		}
		groupCombo.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				Logger.CLIENT_INFO("selected BoxIndex: " + groupCombo.getSelectedIndex());
				if (groupCombo.getSelectedIndex() != 0) {
					presenter.groupSelected(groups.get(groupCombo
							.getSelectedIndex()-1));
					fillSpecies(groups.get(groupCombo.getSelectedIndex()-1)
							.getSpecies().name());
					originTextBox.setText(groups.get(groupCombo.getSelectedIndex()-1)
							.getOrigin());
					noteTextBox.setText(groups.get(groupCombo.getSelectedIndex()-1)
							.getGenehmigung());
				}
			}
		});

	}

	void fillSpecies(String species) {
		speciesBox.setText(species);
	}

	public void setData(ArrayList<TierDTO> result) {
		Logger.CLIENT_INFO("setData " + result.get(0).getInternal_id());
		// IntIdSuggestOracle oracle = new IntIdSuggestOracle(result);
		MultiWordSuggestOracle oracle2 = new MultiWordSuggestOracle();
		for (int i = 0; i < result.size(); ++i) {
			oracle2.add(result.get(i).getInternal_id());
		}

		setSuggestions(oracle2);

	}

	void setSuggestions(MultiWordSuggestOracle oracle) {
		intIdSuggestBox = new SuggestBox(oracle);

	}

	@UiHandler("btn_add_group")
	void onLoginClicked(ClickEvent event) {
		if (presenter != null) {
			presenter.onAddClicked(new TierDTO());
		}
	}

	public void succeeded() {
		btn_add_group.setEnabled(false);
	}

	public void failed() {

	}

	@Override
	public Widget onInitialize() {
		// Create a table to layout the form options
		layout = new FlexTable();
		layout.setCellSpacing(6);
		layout.setWidth("500px");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Add a title to the form
		int i = 0;
		layout.setHTML(i++, 0, "Bitte Daten w" + "&auml;" +"&#228;hlen/eingeben");
		cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		// Add some standard form options
		
		layout.setHTML(i, 0, "Genehmigung");
		layout.setWidget(i++, 1, groupCombo = new ListBox());
		layout.setHTML(i, 0, "Herkunft");
		layout.setWidget(i++, 1, originTextBox = new TextBox());
		layout.setHTML(i, 0, "Interne ID");
		layout.setWidget(i++, 1, intIdSuggestBox = new SuggestBox());
		layout.setHTML(i, 0, "Interne Nummer");
		layout.setWidget(i++, 1, intNrIntBox = new IntegerBox());
		layout.setHTML(i, 0, "Art");
		layout.setWidget(i++, 1, speciesBox = new TextBox());
		layout.setHTML(i, 0, "Eingangsdatum");
		layout.setWidget(i++, 1, eingangDate = new DateBox());
		layout.setHTML(i, 0, "Sterbe/Ausgangsdatum");
		layout.setWidget(i++, 1, dispatchDate = new DateBox());
		layout.setHTML(i, 0, "Geschlecht");

		genderPanel = new HorizontalPanel();
		TierGender[] genderOptions = TierGender.values();
		for (int j = 0; i < genderOptions.length; j++) {
			genderPanel
					.add(new RadioButton("gender", genderOptions[j].gender()));
		}
		layout.setWidget(i++, 1, genderPanel);

		// Create some advanced options
		int k = 0;
		Grid advancedOptions = new Grid(6, 2);
		advancedOptions.setCellSpacing(6);
		advancedOptions.setHTML(k, 0, "Geburtsdatum");
		advancedOptions.setWidget(k++, 1, birthdayDate = new DateBox());
		advancedOptions.setHTML(k, 0, "Stamm");
		advancedOptions.setWidget(k++, 1, stammTextBox = new TextBox());
		advancedOptions.setHTML(k, 0, "Vater");
		advancedOptions.setWidget(k++, 1, fatherCombo = new ListBox());
		advancedOptions.setHTML(k, 0, "Mutter");
		advancedOptions.setWidget(k++, 1, fatherCombo = new ListBox());
		advancedOptions.setHTML(k, 0, "Typus");
		advancedOptions.setWidget(k++, 1, typusText = new TextBox());
		advancedOptions.setHTML(k, 0, "Notiz");
		advancedOptions.setWidget(k++, 1, noteTextBox = new TextArea());

		// Add advanced options to form in a disclosure panel
		DisclosurePanel advancedDisclosure = new DisclosurePanel(
				"Optionale Angaben");
		advancedDisclosure.setAnimationEnabled(true);
		advancedDisclosure.ensureDebugId("cwDisclosurePanel");
		advancedDisclosure.setContent(advancedOptions);
		layout.setWidget(i++, 0, advancedDisclosure);
		cellFormatter.setColSpan(i-1, 0, 2);

		// Wrap the content in a DecoratorPanel
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(layout);
		return decPanel;
	}

	@Override
	protected void asyncOnInitialize(final AsyncCallback<Widget> callback) {
		GWT.runAsync(CV_addTier.class, new RunAsyncCallback() {

			public void onFailure(Throwable caught) {
				callback.onFailure(caught);
			}

			public void onSuccess() {
				callback.onSuccess(onInitialize());
			}
		});

	}

	class IntIdSuggestOracle extends SuggestOracle {

		ArrayList list = new ArrayList<String>();

		public IntIdSuggestOracle(ArrayList<TierDTO> result) {
			for (int i = 0; i < result.size(); ++i) {
				list.add(result.get(i).getInternal_id());
			}
		}

		public void requestSuggestions(Request request, Callback callback) {
			// get query
			String query = request.getQuery();

			if (query.length() >= 3) {

				Response response = new Response(list);
				callback.onSuggestionsReady(request, response);

			}
		}
	}

}
